/**
 * 
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import model.TaiKhoanM;
import util.DAL;
import util.MyConnectUnit;


public class TaiKhoanD {
	private MyConnectUnit conn;
	
	/**
	 * @function constructor TaiKhoanD
	 * @throws Exception 
	 */
	public TaiKhoanD() throws Exception {
		super();
		this.conn=DAL.getDAL();
	}
	
	/**
	 * @function get all TaiKhoan
	 * @param Condition
	 * @param Limit
	 * @param OrderBy
	 * @return
	 * @throws Exception
	 */
	public ArrayList<TaiKhoanM> getAll(String Condition, String Limit, String OrderBy) throws Exception {
		ArrayList<TaiKhoanM> ls = new ArrayList<TaiKhoanM>();
		ResultSet rs = this.conn.Select("TAI_KHOAN", Condition, Limit, OrderBy);
		TaiKhoanM l = null;
		while (rs.next()) {
			l = new TaiKhoanM();
			l.setId_nguoi_dung(rs.getInt("id_nguoi_dung"));
			l.setMa_nguoi_dung(rs.getString("ma_nguoi_dung"));
			l.setTen_nguoi_dung(rs.getString("ten_nguoi_dung"));
			l.setTen_dang_nhap(rs.getString("ten_dang_nhap"));
			l.setMat_khau(rs.getString("mat_khau"));
			l.setNhom_nguoi_dung(rs.getString("nhom_nguoi_dung"));
			ls.add(l);
		}
		return ls;
	}

	/**
	 * @function get all TaiKhoan with Condition + Limit
	 * @param Condition
	 * @param Limit
	 * @return
	 * @throws Exception
	 */
	public ArrayList<TaiKhoanM> getAll(String Condition, String Limit) throws Exception {
		return getAll(Condition, Limit, null);
	}

	/**
	 * @function get all TaiKhoan with Condition
	 * @return
	 * @throws Exception
	 */
	public ArrayList<TaiKhoanM> getAll(String Condition) throws Exception {
		return getAll(Condition, null);
	}
	
	/**
	 * @function get all TaiKhoan no parameter
	 * @return
	 * @throws Exception
	 */
	public ArrayList<TaiKhoanM> getAll() throws Exception {
		return getAll(null);
	}
	
	/**
	 * @function search ten TaiKhoan
	 * @param ten_nguoi_dung
	 * @return
	 * @throws Exception
	 */
	public ArrayList<TaiKhoanM> searchTK(String ten) throws Exception {
		ArrayList<TaiKhoanM> list = getAll("ten_nguoi_dung LIKE '%" + ten + "%'");
		return list;
	}
	
	/**
	 * @function get all TaiKhoan with search + paging
	 * @param from
	 * @param to
	 * @return
	 * @throws Exception
	 */
	public ArrayList<TaiKhoanM> getLimit(String ten, int from, int to) throws Exception {
		String limit = from + ", " + to;
		ArrayList<TaiKhoanM> list = getAll("ten_nguoi_dung LIKE '%" + ten + "%'", limit);
		return list;
	}

	/**
	 * @function lay ten dang nhap
	 * @param ten_nguoi_dung
	 * @return
	 * @throws Exception
	 */
	public TaiKhoanM getTenDangNhap(String user) throws Exception {
		TaiKhoanM tk = new TaiKhoanM();
		ArrayList<TaiKhoanM> list = getAll("ten_dang_nhap = '" + user + "'");
		if(list.size()>0){
			tk.setTen_dang_nhap(list.get(0).getTen_dang_nhap());
			tk.setMat_khau(list.get(0).getMat_khau());
			tk.setNhom_nguoi_dung(list.get(0).getNhom_nguoi_dung());
			return tk;
		}
		return null;
	}
	
	/**
	 * @function check ten dang nhap
	 * @param ten
	 * @return
	 * @throws Exception
	 */
	public int checkTenDangNhap(String ten) throws Exception{
		ArrayList<TaiKhoanM> l = getAll("ten_dang_nhap = '" + ten + "'");
		return l.size();
	}
	
	/**
	 * @function check ten ma nguoi dung
	 * @param ma
	 * @return
	 * @throws Exception
	 */
	public int checkMaNguoidung(String ma) throws Exception{
		ArrayList<TaiKhoanM> l = getAll("ma_nguoi_dung = '" + ma + "'");
		return l.size();
	}
	
	/**
	 * function insert TaiKhoan
	 * @param tk
	 * @return
	 * @throws Exception
	 */
	public boolean Insert(TaiKhoanM tk) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ma_nguoi_dung", tk.getMa_nguoi_dung());
		map.put("ten_nguoi_dung", tk.getTen_nguoi_dung());
		map.put("ten_dang_nhap", tk.getTen_dang_nhap());
		map.put("mat_khau", tk.getMat_khau());
		map.put("nhom_nguoi_dung", tk.getNhom_nguoi_dung());
		return this.conn.Insert("TAI_KHOAN", map);
	}

	/**
	 * @function update TaiKhoan
	 * @param tk
	 * @return
	 * @throws Exception
	 */
	public boolean Update(TaiKhoanM tk) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("ma_nguoi_dung", tk.getMa_nguoi_dung());
		map.put("ten_nguoi_dung", tk.getTen_nguoi_dung());
		map.put("nhom_nguoi_dung", tk.getNhom_nguoi_dung());
		return this.conn.Update("TAI_KHOAN", map, "id_nguoi_dung = " + tk.getId_nguoi_dung());
	}
	
	/**
	 * @function update Password
	 * @param tk
	 * @return
	 * @throws Exception
	 */
	public boolean UpdatePass(TaiKhoanM tk) throws Exception {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("mat_khau", tk.getMat_khau());
		return this.conn.Update("TAI_KHOAN", map, "id_nguoi_dung = " + tk.getId_nguoi_dung());
	}

	/**
	 * @function delete TaiKhoan
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public boolean Delete(int id) throws Exception {
		return this.conn.Delete("TAI_KHOAN", "id_nguoi_dung = " + id);
	}

}